package com.elong.businesstravel.modules.collect;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dslv.DragSortListView;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.dialog.CustomAlertDialog;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.AccountInfo;
import com.elong.businesstravel.entitys.HotelCollect;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.modules.collect.adapter.HotelCollectAdapter;
import com.elong.businesstravel.modules.home.HotelDetailActivity;
import com.elong.businesstravel.modules.member.adapter.MemberGetBonusAdapter;
import com.elong.businesstravel.protocol.hotel.DeleteCollectionRequest;
import com.elong.businesstravel.protocol.hotel.DeleteCollectionResponse;
import com.elong.businesstravel.protocol.hotel.GetCollectionRequest;
import com.elong.businesstravel.protocol.hotel.GetCollectionResponse;
import com.elong.businesstravel.protocol.hotel.UpdatecollectionsequenceRequest;
import com.elong.businesstravel.protocol.hotel.UpdatecollectionsequenceResponse;

/**
 * 酒店收藏
 * 
 * @author lenovo
 * 
 */
public class HotelCollectActivity extends BaseActivity {

	private DragSortListView mDragSortListView;
	private HotelCollectAdapter mHotelCollectAdapter;

	private List<HotelCollect> mHotelCollects = new ArrayList<HotelCollect>();

	private ViewGroup mNoResultLayout;

	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			HotelCollect hotelCollect = (HotelCollect) mHotelCollectAdapter
					.getItem(from);
			mHotelCollectAdapter.remove(from);
			mHotelCollectAdapter.insert(hotelCollect, to);
			mHotelCollectAdapter.notifyDataSetChanged();
			updateCollectionSequence(from, to);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel_collect);
		init();
	}

	@Override
	protected void init() {

		mNoResultLayout = (ViewGroup) findViewById(R.id.noResultLayout);

		mDragSortListView = (DragSortListView) findViewById(R.id.listView);
		mDragSortListView.setDropListener(onDrop);
		mHotelCollectAdapter = new HotelCollectAdapter(mContext,
				mHotelCollects, false);
		mDragSortListView.setAdapter(mHotelCollectAdapter);

		setTitleNavBar();
		setListener();

		getCollection();
	}

	private void noResult() {
		mNoResultLayout.setVisibility(View.VISIBLE);
		mDragSortListView.setVisibility(View.GONE);
	}

	private void haveResult() {
		mNoResultLayout.setVisibility(View.GONE);
		mDragSortListView.setVisibility(View.VISIBLE);
	}

	@Override
	protected void setListener() {
		mDragSortListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HotelCollect hotelCollect = (HotelCollect) mHotelCollectAdapter
						.getItem(arg2);
				HotelDocs hotelDocs = new HotelDocs();
				hotelDocs.hotelName = hotelCollect.hotelname;
				hotelDocs.hotelId = hotelCollect.hotelid;
				hotelDocs.hotelAddress = hotelCollect.address;

				if (mHotelCollectAdapter.getDeleteFlag()) {
					showDeleteDialog(hotelCollect.hotelid, arg2);
				} else {
					Intent intent = new Intent(mContext,
							HotelDetailActivity.class);
					intent.putExtra("hotelDocs", hotelDocs);
					startActivityForResult(intent, 0);
				}
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("收藏的酒店");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		if (0 == mHotelCollects.size()) {
			titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
		} else {
			titleNavBarView.setRightButtonVisibility(View.VISIBLE);
			if (mHotelCollectAdapter.getDeleteFlag()) {
				// titleNavBarView.setRightButtonImageString(0, "删除",
				// R.color.text_blue);
				// titleNavBarView
				// .setRightButtonOnClickListener(new OnClickListener() {
				//
				// @Override
				// public void onClick(View arg0) {
				// StringBuilder stringBuilder = new StringBuilder();
				// Map<Integer, Boolean> map = mHotelCollectAdapter
				// .getDeleteMap();
				// for (int i = 0; i < mHotelCollects.size(); i++) {
				// HotelCollect hotelCollect = mHotelCollects
				// .get(i);
				// if (map.get(i)) {
				// stringBuilder.append(
				// hotelCollect.hotelid).append(
				// ",");
				// }
				// }
				// stringBuilder.delete(
				// stringBuilder.length() - 1,
				// stringBuilder.length());
				// showDeleteDialog(stringBuilder.toString());
				// }
				// });
				titleNavBarView.setRightButtonImageString(0, "完成",
						R.color.text_blue);
				titleNavBarView
						.setRightButtonOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View arg0) {
								mHotelCollectAdapter.setDeleteFlag(false);
								mHotelCollectAdapter.notifyDataSetChanged();
								setTitleNavBar();
							}
						});
			} else if (!mHotelCollectAdapter.getDeleteFlag()) {
				titleNavBarView.setRightButtonImageString(0, "编辑",
						R.color.text_blue);
				titleNavBarView
						.setRightButtonOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View arg0) {
								mHotelCollectAdapter.setDeleteFlag(true);
								mHotelCollectAdapter.notifyDataSetChanged();
								setTitleNavBar();
							}
						});
			}
		}

	}

	/**
	 * 获取收藏列表
	 */
	private void getCollection() {
		NetworkClient.getNetworkClient().PostRequest(
				new GetCollectionRequest(mContext),
				new GetCollectionResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mHotelCollects.clear();
								mHotelCollects.addAll(hotelCollects);
								if (0 == mHotelCollects.size()) {
									// 没有收藏
									noResult();
								} else {
									haveResult();
								}
								mHotelCollectAdapter.setDeleteFlag(false);
								mHotelCollectAdapter.notifyDataSetChanged();
								setTitleNavBar();
							}
						});

					}
				});
	}

	/**
	 * 删除收藏酒店
	 * 
	 * @param hotelid
	 *            ”，“分割
	 */
	private void deletecollection(String hotelid, final int position) {

		NetworkClient.getNetworkClient().PostRequest(
				new DeleteCollectionRequest(mContext, hotelid),
				new DeleteCollectionResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if (0 == status) {
							if (!TextUtils.isEmpty(messageString)) {
								HandlerToastUI.getHandlerToastUI(mContext,
										"取消收藏" + messageString);
							}
							// getCollection();
							HandlerPostUI.getHandlerPostUI(new Runnable() {

								@Override
								public void run() {
									mHotelCollects.remove(position);
									mHotelCollectAdapter.notifyDataSetChanged();
								}
							});
						} else {
							HandlerToastUI
									.getHandlerToastUI(mContext, "取消收藏失败");
						}
					}
				});
	}

	private void showDeleteDialog(final String hotelid, final int position) {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog.setMessage("是否删除选中收藏的酒店");
		customAlertDialog.setLeftButton("删除", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				deletecollection(hotelid, position);
			}
		});
		customAlertDialog.setRightButton("取消", new OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
		customAlertDialog.show();
	}

	public void updateCollectionSequence(int from, int to) {
		StringBuilder hotelIdStringBuilder = new StringBuilder();
		for (int i = 0; i < mHotelCollects.size(); i++) {
			HotelCollect hotelCollect = mHotelCollects.get(i);
			hotelIdStringBuilder.append(hotelCollect.hotelid).append(",");
		}
		hotelIdStringBuilder.delete(hotelIdStringBuilder.length() - 1,
				hotelIdStringBuilder.length());

		StringBuilder sequencearrayStringBuilder = new StringBuilder();
		for (int i = 0; i < mHotelCollects.size(); i++) {
			int temp = i;
//			if (i == from) {
//				temp = to;
//			} else if (i == to) {
//				temp = from;
//			}
			sequencearrayStringBuilder.append(temp + "").append(",");
		}
		sequencearrayStringBuilder.delete(
				sequencearrayStringBuilder.length() - 1,
				sequencearrayStringBuilder.length());

		DebugLog.logI("sequencearrayStringBuilder : "
				+ sequencearrayStringBuilder.toString());

		NetworkClient.getNetworkClient().PostRequest(
				new UpdatecollectionsequenceRequest(mContext,
						hotelIdStringBuilder.toString(),
						sequencearrayStringBuilder.toString()),
				new UpdatecollectionsequenceResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {

					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 进入详情界面，更新收藏返回进入
			switch (resultCode) {
			case RESULT_OK: {
				getCollection();
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
		}

			break;

		default:
			break;
		}
	}

}
